<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>inote 云记事</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <link rel="stylesheet" type="text/css" href="../css/layout.css" />
    <link rel="stylesheet" type="text/css" href="../css/component/bar.css" />
    <link rel="stylesheet" type="text/css" href="../css/component/panel.css"/>
    <style>
    	html,body{
            width: 100%;
            height: 100%;
        }
        #wrap{
            height: 100%;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-flex-flow: column;
            width: 100%;
        }
        #header{
            position: fixed;
        }
        #main{
            -webkit-box-flex: 1; 
            -webkit-flex: 1;  
            padding-top: 48px;
        }
        .css-test{
            width: 50px;
            height: 50px;
            margin-top: 10px;
            background-color: #2196F3;
            border-radius: 100%;
        }
        .tabActive{
            background-color: #42A5F5;
        }
        .hide{
            display: none;
        }
        .con{
            padding: 15px 8px;
            color: #212121;
        }
        .con-header{
            width: 100%;
            height: auto;
        }
        .ino-panel{
            margin-bottom: 12px;
        }
        .main{
            padding: 0px;
            height: auto;
            font-size: 16px;
            line-height: 24px;
        }
        .main-title,.main-list{
            /*padding: 15px;*/
            height: 54px;
        }
        .main-title{
            background-color: rgba(0, 0, 0, 0.12);
        }
        .main-list{
            position: relative;
        }
        .date{
            float: left;
            width: 90px;
            height: 54px;
            text-align: center;
        }
        .date > strong{
            height: 54px;
            line-height: 54px;
        }
        .date > h4{
            font-weight: bold;
            font-size: 16px;
            margin: 0px;
            margin-top: 6px;
        }
        .date > span{
            font-size: 12px;
        }
        .detail{
            float: left;
            height: 54px;
            line-height: 54px;
            padding-left: 21px;
        }
        .more{
            position: absolute;
            top: 0px;
            right: 0px;
            padding: 15px;
        }
        .main-list{
            border-top: 1px solid rgba(0, 0, 0, 0.12);
        }
    </style>
</head>
<body>
    <div id="wrap">
        <div id="header" class="ino-bar home-bar">
            <div class="title">inote</div>
            <table class="ino-bar-content">
                <tr>
                    <td>
                        <div class="bar-tab bar-tab-back" tapmode="tabActive" onclick="api.closeWin();">
                            <img class="listTab" src="../image/png/webpage/arrow_back.png">
                        </div>                        
                    </td>
                    <td class="bar-right repeatAdd" align="right">
                        <div class="bar-tab bar-tab-menu" tapmode="tabActive" onclick="addSchedule()">
                            <img class="listTab" src="../image/png/webpage/add.png">
                        </div>
                    </td>
                </tr>
            </table>
        </div>
        <div id="main">
            <div class="con">
                <div class="con-main">
                    
                </div> 
            </div>
        </div>
    </div>
        	
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript">
    function openToast(msg) {
        api.toast({
            msg: msg,
            duration: 1500,
            location: 'middle'
        });
    };
	apiready = function(){
        writePagename();
		var db = api.require('db');
        db.openDatabase({
            name: 'inote',
            path: 'fs://inote.db'
        }, function(ret, err){
            if(ret.status){
                // 相关的操作放在这里
                sel('Schedule');
                closeDatabase();
            }
        });
        // =============================================
        // 打开inote.db
        // 插入新的数据，删除数据，修改数据,一次一条
        // 查询数据并读取出来，一次多条
        function closeDatabase() {
            db.closeDatabase({name:'inote'});     
        };
        function selectSql(sql) {
            db.selectSql({
                name:'inote',
                sql: sql
            }, function(ret, err){
                if(ret.status){
                    var datas = ret.data;
                    if (datas.length === 0) {
                        // alert('快添加任务吧');
                        openToast('快添加每日任务吧');
                        // 这里添加提示框
                    }else{
                        writeTaskTitle();
                        // alert( JSON.stringify( ret ) );
                        getSingleData(datas);
                        addTimeStamp(schedule);
                        schedule.sort(function(a,b){return a[7]-b[7]});
                        for (var i = 0; i < schedule.length; i++) {
                            writeListTask(schedule[i]);
                        };
                        // alert(schedule);
                    };
                }
            });
        };
        // =============================================
        // 具体db的操作
        // @table, @id, @type, @detail
        function sel(table) {
            var sql = "SELECT * FROM "+table+" WHERE type = '1'";
            selectSql(sql);
        };
	};
    // =====================================================
    // 添加标题
    function writePagename() {
        var pagename = api.pageParam.pagename;
        $api.dom('.title').innerHTML = pagename;
    };
    // 数据处理
    var schedule = new Array();
    function getSingleData(datas) {
        for (var i = 0; i < datas.length; i++) {
            var schedule_datas = new Array();
            schedule_datas[0] = datas[i].id;
            schedule_datas[1] = datas[i].type;
            schedule_datas[2] = datas[i].status;
            schedule_datas[3] = datas[i].title;
            schedule_datas[4] = datas[i].detail;
            schedule_datas[5] = datas[i].startTime;
            schedule_datas[6] = datas[i].endTime;   
            schedule.push(schedule_datas);
            // alert(getAllTodayDate());
        };
    };
    // 加入时间戳
    function addTimeStamp(arr) {
        for (var i = 0; i < arr.length; i++) {
            var timestamp = arr[i][5].replace(/:/g, "");
            // var timestamp = (new Date(arr[i][4])).valueOf().toString();
            // 字符替换或者Date时间戳，后进行排序
            arr[i].push(timestamp);
            // alert(arr[i]);
        };
        return arr;
    };
    // 写入标题
    function writeTaskTitle() {
        var todayTaskTitle = '<div class="ino-panel main" id="task"><div class="main-title"><div class="date"><strong>每日</strong></div><div class="detail"><strong id="sum"></strong>件计划任务</div></div></div>';
        $api.append($api.dom('.con-main'), todayTaskTitle);
    };
    // 插入具体数据
    function writeListTask(datas) {
        var mainId = $api.dom('#task');
        var listId = 'list'+datas[7];
        var dayMoment = timeBucket(datas[5]);
        var thisTitle = datas[3];
        var thisTime = timeExtent(datas[5], datas[6]);
        var status = 'chevron_right';
        if (datas[2] === '1') {
            var status = 'check';
        };

        var tistTask = '<div class="main-list" id="'+listId+'" onclick="openDetailPage('+datas[0]+')"><div class="date"><h4>'+dayMoment+'</h4><span>'+thisTime+'</span></div><div class="detail">'+thisTitle+'</div><div class="more"><img src="../image/png/'+status+'.png" width="24"></div></div>';
        $api.append(mainId, tistTask);
        sumTask(listId);
    };
    // 计算当天的任务数量并且写入标题栏
    var checkSumTask;
    var allList = 0;
    function sumTask(listId) {
        
        if (listId === checkSumTask) {
            allList = allList+1;
            $api.dom('#sum').innerHTML = allList;
            // alert(allList);
        }else{
            checkSumTask = listId;
            allList = 1;
            $api.dom('#sum').innerHTML = allList;
            // alert(allList);
        }
    };
    // 计算时间差
    function timeExtent(s, e) {
        var st = s;
        var et = e;
        return st+'-'+et;
    };
    // 划分时间段
    function timeBucket(s) {
        var st = parseInt(s.substr(11, 2));
        if (st >= 6 && st <= 8) {
            return '早上';
        }else if (st > 8 && st <= 11){
            return '上午';
        }else if (st > 11 && st <= 14){
            return '中午';
        }else if (st > 14 && st <= 17){
            return '下午';
        }else{
            return '晚上';
        }
    };

    // 打开任务的详情页
    function openDetailPage(id) {
        api.openWin({
            name: 'add_repeat_schedule',
            url: 'add_repeat_schedule.html',
            pageParam: {
                name: 'repeatDetailPage',
                pagename: '任务详情',
                taskId: id
            }
        });
        // alert(id);
    };  
    function addSchedule() {
        api.openWin({
            name: 'add_repeat_schedule',
            url: 'add_repeat_schedule.html',
            pageParam: {
                name: 'add_repeat_schedule',
                pagename: '添加每日任务',
                taskId: '0'
            }
        });
    };
</script>
</html>